package com.shenfa.action.systemManager;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.action.BaseAction;
import com.shenfa.bean.Function;
import com.shenfa.service.systemManager.function.FunctionService;
import com.shenfa.util.ResultObject;

/**
 * 功能Action
 * 
 * @author yanglong
 * 
 * @date 2011-8-27
 *
 */
public class FunctionAction  extends BaseAction{

	private static final long serialVersionUID = 1L;

	@Autowired
	private FunctionService functionService;
	
	private static Logger log=Logger.getLogger(FunctionAction.class);
	private Function func;
	private int start;
	private int limit;
	public void setStart(int start) {
		this.start = start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
	/**
	 * 根据对象条件查询功能集合
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		log.debug("start="+start+",limit="+limit+",func="+func);
		List<Function> list = functionService.getList(func,start,limit);
		result = ResultObject.build("0", list);
		result.put("total", functionService.queryCount(func));
		return SUCCESS;
	}
	
	/**
	 * 获取功能序列号
	 */
	public String queryFuncNo() {
		String funcNo = functionService.queryFuncNo();
		result = ResultObject.build("1", Integer.parseInt(funcNo));
		return SUCCESS;
	}
	
	/**
	 * 添加功能
	 * 
	 * @author yanglong
	 * 
	 * @date 2011-8-27
	 */
	public String add() {
		log.debug("id="+func.getId()+",level="+func.getLevel()+",name="+func.getName()+",parent="+func.getParent()+",remark="+func.getRemark()
				+",state="+func.getState()+",url="+func.getUrl());
		int type = functionService.add(func);
		if(type == 1){
			result = ResultObject.build(String.valueOf(type), "添加成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}
	
	/**
	 * 删除功能
	 */
	public String del() {
		log.debug("id="+func.getId());
		int type = functionService.del(func);
		if(type == 1){
			result = ResultObject.build(String.valueOf(type), "删除成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}
	
	/**
	 * 修改功能
	 * @return
	 */
	public String upd(){
		log.debug("id="+func.getId()+",level="+func.getLevel()+",name="+func.getName()+",parent="+func.getParent()+",remark="+func.getRemark()
				+",state="+func.getState()+",url="+func.getUrl());
		int type = functionService.upd(func);
		if(type == 1){
			result = ResultObject.build(String.valueOf(type), "修改成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}

	public Function getFunc() {
		return func;
	}

	public void setFunc(Function func) {
		this.func = func;
	}
	
}
